import index from "../views/index.ejs";
import Navbar from "../components/Navbar.ejs"
import Sidebar from "../components/Sidebar.ejs"
import Footer from "../components/Footer.ejs"
import Content from "../components/Content.ejs"
import {sidebarRoutes} from "../router";
// 单击事件的处理函数
const sideBarMenuHanlder = function(router,e){
	const target = e.target.parentNode.classList.contains("nav-link")?e.target.parentNode:e.target;
	router.go(target.dataset.go);
}

export default function(router){
	return ({url},res,next)=>{
		next(index({
			Navbar:Navbar(),
			Sidebar:Sidebar({url,sidebarRoutes}),
			Footer:Footer(),
			Content:Content({
				subRoute:res.subRoute(),
				pageTitle:sidebarRoutes.find(v=>v.path===url).title
			}),
		}));
		// 获取侧边栏按钮的父级元素，通过该元素可以实现事件委托
		const sidebarMenu = document.querySelector("#sidebarMenu");
		// 增加事件
		sidebarMenu.onclick = sideBarMenuHanlder.bind(null,router);
		document.querySelector("#outLogin").onclick = function(){
			localStorage.clear();
			router.go("/login");
		}
		
	};
}